package com.quasar.hpatient.module.mine_info;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumPhotoBean;
import com.quasar.hpatient.dialog.DateDialog;
import com.quasar.hpatient.dialog.RadioDialog;
import com.quasar.hpatient.dialog.RadioDoubleDialog;
import com.quasar.hpatient.dialog.RadioThreeDialog;
import com.quasar.hpatient.module.comm_album.AlbumActivity;
import com.quasar.hpatient.module.comm_camera.CameraActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.Aerainfo;
import lib.quasar.db.table.Hospital;
import lib.quasar.db.table.User;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 我 ==> 个人资料
 * created by kalu on 2018/7/16 15:41
 */
public final class MineInfoActivity extends BaseActivity<MineInfoPresenter> implements MineInfoView {

    public final static int REQUEST_CODE = 1001;

    @Override
    public int initView() {
        return R.layout.activity_mine_info;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = findViewById(R.id.patient_info_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else {

                final ImageView image = findViewById(R.id.mine_info_icon);
                final TextView name = findViewById(R.id.patient_detail_name);
                final TextView sex = findViewById(R.id.mine_info_sex);
                final TextView birthday = findViewById(R.id.mine_info_birthday);
                final TextView blood = findViewById(R.id.mine_info_blood);
                final TextView master = findViewById(R.id.mine_info_master);
                final TextView sample = findViewById(R.id.mine_info_sample);
                final TextView type = findViewById(R.id.mine_info_type);
                final TextView from = findViewById(R.id.mine_info_from);
                final TextView time = findViewById(R.id.mine_info_time);
                final TextView hospital = findViewById(R.id.mine_info_hospital);
                final TextView phone = findViewById(R.id.mine_info_phone);
                final TextView local = findViewById(R.id.mine_info_local);
                final TextView card = findViewById(R.id.mine_info_card);

                getPresenter().pushInfo(this,
                        card.getText().toString(),
                        name.getText().toString(),
                        ((String) image.getTag(-110)),
                        sex.getText().toString(),
                        birthday.getText().toString(),
                        blood.getText().toString(),
                        master.getText().toString(),
                        sample.getText().toString(),
                        type.getText().toString(),
                        from.getText().toString(),
                        time.getText().toString(),
                        hospital.getText().toString(),
                        phone.getText().toString(),
                        local.getText().toString());
            }
        });

        findViewById(R.id.mine_info_icon).setOnClickListener(v -> {
            final Intent intent = new Intent(getApplicationContext(), AlbumActivity.class);
            intent.putExtra(AlbumActivity.CAMERA, true);
            startActivityForResult(intent, REQUEST_CODE);
        });

        final TextView sex = findViewById(R.id.mine_info_sex);
        sex.setOnClickListener(v -> showRadio(sex, Arrays.asList("男", "女"), "选择性别"));

        final TextView birthday = findViewById(R.id.mine_info_birthday);
        birthday.setOnClickListener(v -> showDate(birthday, "选择出生日期"));

        final TextView blood = findViewById(R.id.mine_info_blood);
        blood.setOnClickListener(v -> showRadio(blood, Arrays.asList("A型,RH阳性", "A型,RH阴性", "B型,RH阳性", "B型,RH阴性", "AB型,RH阳性"), "选择血型"));

        final TextView sample = findViewById(R.id.mine_info_sample);
        sample.setOnClickListener(v -> showRadio(sample, Arrays.asList("供体", "受体"), "选择供受体"));

        final TextView type = findViewById(R.id.mine_info_type);
        type.setOnClickListener(v -> showRadio(type, Arrays.asList("肾移植", "肝移植", "肺移植", "心移植"), "选择移植类型"));

        final TextView from = findViewById(R.id.mine_info_from);
        from.setOnClickListener(v -> showRadio(from, Arrays.asList("亲属活体肾移植", "公民逝世器官捐献(DD)"), "选择器官来源"));

        final TextView time = findViewById(R.id.mine_info_time);
        time.setOnClickListener(v -> showDate(time, "选择手术日期"));

        final TextView hospital = findViewById(R.id.mine_info_hospital);
        hospital.setOnClickListener(v -> showHospital(hospital, "选择医院"));

        final TextView local = findViewById(R.id.mine_info_local);
        local.setOnClickListener(v -> showLocal(local, "选择地址"));
    }

    @Override
    public void initDataNet() {

        getPresenter().loadInfo(this);
    }

    @Override
    public void showRadio(TextView view, List<String> list, String title) {

        final RadioDialog dialog = new RadioDialog(this);
        dialog.show();
        dialog.setList(list);
        dialog.setLoop(false);
        dialog.setTitle(title);
        dialog.setOnMethodChangeListener(method -> view.setText(method));
    }

    @Override
    public void showDate(TextView view, String title) {

        final DateDialog dialog = new DateDialog(this);
        dialog.show();
        dialog.setTitles(title);
        dialog.setOnDateChangeListener((year, month, day) -> view.setText(year + "-" + month + "-" + day));
    }

    @Override
    public void showHospital(TextView view, String title) {

        final List<String> cityList = new ArrayList<>();
        final List<String> hospitalList = new ArrayList<>();
        final RadioDoubleDialog dialog = new RadioDoubleDialog(this);

        dialog.setOnDialogChangeListener((str1, str2, iswheel) -> {
            if (!iswheel) {
                setText(view, str2);
            } else if (!TextUtils.isEmpty(str1)) {
                String areaCode = DBManager.getInstance().getAreaCode(str1);
                final List<Hospital> list = DBManager.getInstance().getHospitalList(areaCode);
                hospitalList.clear();
                for (Hospital temp : list) {
                    hospitalList.add(temp.getName());
                }
                dialog.setList(null, hospitalList);
            }
        });
        dialog.show();

        final List<Aerainfo> list = DBManager.getInstance().getProvinceList();
        for (Aerainfo temp1 : list) {
            cityList.add(temp1.getAeraInfoName());
        }
        dialog.setList(cityList, null);
        dialog.setTitle(title);
    }

    @Override
    public void showLocal(TextView view, String title) {

        final List<String> list1 = new ArrayList<>();
        final List<String> list2 = new ArrayList<>();
        final List<String> list3 = new ArrayList<>();

        final RadioThreeDialog dialog = new RadioThreeDialog(this);
        dialog.setOnDialogChangeListener((str1, str2, str3, iswheel) -> {
            // LogUtil.e("showAddress ==> ", str1 + ", " + str2 + ", " + str3 + ", iswheel = " + iswheel);

            if (!iswheel) {
                setText(view, str1 + str2 + str3);
            } else if (!TextUtils.isEmpty(str2)) {
                final String areaCode = DBManager.getInstance().getAreaCode(str2);
                final List<Aerainfo> prefectureList = DBManager.getInstance().getAreaList(areaCode);
                list3.clear();
                for (Aerainfo model : prefectureList) {
                    list3.add(model.getAeraInfoName());
                }
                dialog.setList(null, null, list3);
            } else if (!TextUtils.isEmpty(str1)) {
                final String areaCode = DBManager.getInstance().getAreaCode(str1);
                final List<Aerainfo> cityList = DBManager.getInstance().getAreaList(areaCode);
                list2.clear();
                for (Aerainfo model : cityList) {
                    list2.add(model.getAeraInfoName());
                }
                dialog.setList(null, list2, null);
            }

        });
        dialog.show();
        dialog.setTitle(title);

        final List<Aerainfo> provinceList = DBManager.getInstance().getProvinceList();
        for (Aerainfo model : provinceList) {
            list1.add(model.getAeraInfoName());
        }
        dialog.setList(list1, null, null);
    }

    @Override
    public void setInfo(User user) {

        final ImageView image = findViewById(R.id.mine_info_icon);
        setImage(image, user.getUrl(), true);
        final TextView name = findViewById(R.id.patient_detail_name);
        setText(name, user.getReal_name());
        final TextView sex = findViewById(R.id.mine_info_sex);
        setText(sex, user.getSexs());
        final TextView birthday = findViewById(R.id.mine_info_birthday);
        setText(birthday, user.getBirthday());
        final TextView blood = findViewById(R.id.mine_info_blood);
        setText(blood, user.getBlood_type());
        final TextView master = findViewById(R.id.mine_info_master);
        setText(master, user.getSupervisor_doctor());
        final TextView sample = findViewById(R.id.mine_info_sample);
        setText(sample, user.getDonor_receptor_type());
        final TextView type = findViewById(R.id.mine_info_type);
        setText(type, user.getTransplant_type());
        final TextView from = findViewById(R.id.mine_info_from);
        setText(from, user.getSource_type());
        final TextView time = findViewById(R.id.mine_info_time);
        setText(time, user.getOperation_date());
        final TextView hospital = findViewById(R.id.mine_info_hospital);
        setText(hospital, user.getHospital_name());
        final TextView phone = findViewById(R.id.mine_info_phone);
        setText(phone, user.getPhone());
        final TextView local = findViewById(R.id.mine_info_local);
        setText(local, user.getAddress());
        final TextView card = findViewById(R.id.mine_info_card);
        setText(card, user.getCards());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == AlbumActivity.RESULT_CODE_OPEN_CAMERA) {
            Intent intent = new Intent(getApplicationContext(), CameraActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        } else if (resultCode == AlbumActivity.RESULT_CODE && null != data) {
            ArrayList<AlbumPhotoBean> list = data.getParcelableArrayListExtra(AlbumActivity.LIST);
            if (null == list || list.size() == 0) return;
            final ImageView image = findViewById(R.id.mine_info_icon);
            getPresenter().compressPhoto(this, image, list.get(0));
        } else if (resultCode == CameraActivity.RESULT_CODE && null != data) {
            final String path = data.getStringExtra(CameraActivity.PATH);
            final long size = data.getLongExtra(CameraActivity.SIZE, -1);
            final int width = data.getIntExtra(CameraActivity.WIDTH, -1);
            final int height = data.getIntExtra(CameraActivity.HEIGHT, -1);
            if (TextUtils.isEmpty(path)) {
                // ToastUtil.showToast("出现未知错误");
                return;
            }
            //  LogUtil.e("photo", "path = " + path + ", size = " + size + ", width = " + width + ", height = " + height);

            AlbumPhotoBean model = new AlbumPhotoBean();
            model.setPicture(path);
            model.setWidth(width);
            model.setHeight(height);
            model.setSize(size);

            final ImageView image = findViewById(R.id.mine_info_icon);
            getPresenter().uploadPhoto(this, image, model);
        }
    }
}